/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgram.platf.message;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.PrivateKey;
import java.util.concurrent.CompletableFuture;

import org.redkale.service.RetResult;
import org.redkale.source.DataSource;
import org.redkale.util.Comment;
import org.redkale.util.Utility;

import com.cratos.xxgram.platf.file.FileService;
import com.cratos.xxgram.platf.group.GroupDetail;
import com.cratos.xxgram.platf.message.BasicContent.AudioMessage;
import com.cratos.xxgram.platf.message.BasicContent.FileMessage;
import com.cratos.xxgram.platf.message.BasicContent.GroupContentMessage;
import com.cratos.xxgram.platf.message.BasicContent.ImageMessage;
import com.cratos.xxgram.platf.message.BasicContent.LocationMessage;
import com.cratos.xxgram.platf.message.BasicContent.RichContentMessage;
import com.cratos.xxgram.platf.message.BasicContent.ShareCardNotify;
import com.cratos.xxgram.platf.message.BasicContent.ShareFileMessage;
import com.cratos.xxgram.platf.message.BasicContent.SightMessage;
import com.cratos.xxgram.platf.message.BasicContent.TextMessage;
import com.cratos.xxgram.platf.message.BasicContent.TipMessage;
import com.cratos.xxgram.platf.user.UserDetail;
import com.cratos.xxgram.platf.util.Cryptos;

/**
 *
 * @author SquRab
 */
public abstract class BasicContentHandler extends MessageHandler {

	@Comment("文件类型消息")
	public static class FileMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new FileMessageHandler();

		@Override
		protected RetResult validate(MessageService messageService, MessageRecord record) {
			// TODO Auto-generated method stub
			return super.validate(messageService, record);
		}

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}
			return feature;
		}

		@Override
		public Class getContentClass() {
			return FileMessage.class;
		}

	}

	@Comment("文件类型消息")
	public static class ImageMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new ImageMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}
			return feature;
		}

		@Override
		public Class getContentClass() {
			return ImageMessage.class;
		}

	}

	@Comment("小视频消息")
	public static class SightMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new SightMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}
			return feature;
		}

		@Override
		public Class getContentClass() {
			return SightMessage.class;
		}

	}

	@Comment("音频消息")
	public static class AudioMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new SightMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				AudioMessage content = jsonConvert.convertFrom(getContentClass(),
						messageService.decodeContent(record).getContent());
				record.setContentObj(content);
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}
			return feature;
		}

		@Override
		public Class getContentClass() {
			return AudioMessage.class;
		}
	}

	@Comment("文字消息")
	public static class TextMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new TextMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}

			return feature;
		}

		@Override
		public Class getContentClass() {
			return TextMessage.class;
		}

	}

	@Comment("位置消息")
	public static class LocationMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new LocationMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}

			return feature;
		}

		@Override
		public Class getContentClass() {
			return LocationMessage.class;
		}

	}

	@Comment("图文消息")
	public static class RichContentMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new RichContentMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}

			return feature;
		}

		@Override
		public Class getContentClass() {
			return RichContentMessage.class;
		}

	}

	@Comment("群聊自定义消息")
	public static class GroupContentMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new GroupContentMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}

			return feature;
		}

		@Override
		public Class getContentClass() {
			return GroupContentMessage.class;
		}

	}

	@Comment("TIP 提示消息 ")
	public static class TipMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new TipMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			try {
				sendMessage(messageService, record);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}

			return feature;
		}

		@Override
		public Class getContentClass() {
			return TipMessage.class;
		}

	}

	@Comment("分享文件内容")
	public static class ShareFileMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new ShareFileMessageHandler();

		@Override
		public CompletableFuture handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture();
			DataSource source = messageService.getSource();
			FileService fileService = messageService.getFileService();
			Cryptos.ECDHCrypto crypto = fileService.getCrypto();
			try {
				int fromid = record.getFromid();
				int targetid = record.getTargetid();
				ShareFileMessage content = (ShareFileMessage) record.getContentObj();
				String privateKeyStr = "";
				String publicKeyStr = "";
				if (ShareFileMessage.GROUP_TYPE == content.getFromType()) {
					GroupDetail groupDetail = source.find(GroupDetail.class, fromid);
					privateKeyStr = groupDetail.getEcdhprivkey();
				}
				if (ShareFileMessage.USER_TYPE == content.getFromType()) {
					UserDetail userDetail = source.find(UserDetail.class, fromid);
					privateKeyStr = userDetail.getEcdhprivkey();
				}
				PrivateKey privateKey = crypto.readPrivateKey(privateKeyStr);
				// 原始文件内容
				byte[] srcFileBytes = Utility.getHttpBytesContent(content.getFileUrl());
				byte[] decryptBytes = crypto.decryptBytes(srcFileBytes, privateKey);
				if (ShareFileMessage.GROUP_TYPE == content.getTargetType()) {
					GroupDetail groupDetail = source.find(GroupDetail.class, fromid);
					publicKeyStr = groupDetail.getEcdhpubkey();
				}
				if (ShareFileMessage.USER_TYPE == content.getTargetType()) {
					UserDetail userDetail = source.find(UserDetail.class, fromid);
					publicKeyStr = userDetail.getEcdhpubkey();
				}
				String fileName = Utility.uuid();
				File storeEncryptFile = fileService.storeEncryptFile("" + targetid, fileName, content.getName(),
						new ByteArrayInputStream(decryptBytes), publicKeyStr);
				// 新文件发送给另外一个用户
				content.setName(fileName);
				// 字节
				int size = (int) storeEncryptFile.length() / 1024;
				content.setSize(size);
				MessageRecord sendRecord = new MessageRecord();
				sendRecord.setFromid(fromid);
				sendRecord.setTargetid(targetid);
				sendRecord
						.setContent(crypto.encrypt(jsonConvert.convertTo(content), crypto.readPublicKey(publicKeyStr)));
				source.insert(sendRecord);// 先保存再发送
				sendMessage(messageService, sendRecord);// 发送消息
				feature.complete(record);
			} catch (Exception e) {
				feature.completeExceptionally(e);
			}
			return feature;
		}

		@Override
		public Class getContentClass() {
			return ShareFileMessage.class;
		}

	}

	@Comment("分享用户或群名片")
	public static final class ShareCardHandler extends BasicContentHandler {

		public static final MessageHandler instance = new ShareCardHandler();

		@Override
		public CompletableFuture<MessageRecord> handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture<MessageRecord>();
			sendMessage(messageService, record);// 发送消息
			feature.complete(record);
			return feature;
		}

		@Override
		protected Class getContentClass() {
			return ShareCardNotify.class;
		}
	}

	@Comment("融云语音消息处理")
	public static final class RongCloudVoiceHandler extends BasicContentHandler {

		public static final MessageHandler instance = new RongCloudVoiceHandler();

		@Override
		public CompletableFuture<MessageRecord> handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture<MessageRecord>();
			sendMessage(messageService, record);// 发送消息
			feature.complete(record);
			return feature;
		}

		@Override
		protected Class getContentClass() {
			return BasicContent.RongCloudVoiceMessage.class;
		}
	}

	@Comment("融云语音消息处理")
	public static final class SysNoticeHandler extends BasicContentHandler {

		public static final MessageHandler instance = new SysNoticeHandler();

		@Override
		public CompletableFuture<MessageRecord> handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture<MessageRecord>();
			sendMessage(messageService, record);// 发送消息
			feature.complete(record);
			return feature;
		}

		@Override
		protected Class getContentClass() {
			return BasicContent.SysNoticeMessage.class;
		}
	}

	@Comment("消息合并转发")
	public static final class MeargeFrowordMessageHandler extends BasicContentHandler {

		public static final MessageHandler instance = new MeargeFrowordMessageHandler();

		@Override
		public CompletableFuture<MessageRecord> handler(MessageService messageService, MessageRecord record) {
			CompletableFuture<MessageRecord> feature = new CompletableFuture<MessageRecord>();
			sendMessage(messageService, record);// 发送消息
			feature.complete(record);
			return feature;
		}

		@Override
		protected Class getContentClass() {
			return BasicContent.MerageForwordMessage.class;
		}
	}
	
	public static void main(String[] args) throws IOException {
		byte[] httpBytesContent = Utility.getHttpBytesContent("http://xxgram.com/app/test.jpeg");
		File f = new File("D:/aax.png");
		FileOutputStream os = new FileOutputStream(f);
		os.write(httpBytesContent);
	}
}
